/*
 * corsixth-for-android is free software, distributed under the following terms:
 * 
 * Copyright (C) 2010 Nicolas Guillaume.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * See LICENSE.txt
 */
package fr.nghs.android.thport;

import org.libsdl.app.SDLActivity;
import org.libsdl.app.SDLSurface;

import fr.nghs.android.common.ScaleGestureDetector;
import fr.nghs.android.thport.gestures.*;
import android.content.Context;
import android.view.KeyEvent;
import android.view.MotionEvent;
/**
 * 
 * @author ng <android@nghs.fr>
 *
 */
public class THSurface
        extends SDLSurface
{
    private THGame game;
    private ScaleGestureDetector scaleDetector;
    private long lastBackKeyDown = -1;   

    public THSurface(Context context, THGame g)
    {
        super(context);
        game = g;
        scaleDetector = new ScaleGestureDetector(context, new SurfaceMoveAndDiscreteScale(this));
        setKeepScreenOn(true);
    }

    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent e)
    { 
        switch(keyCode)
        {
            //TODO when changing to Android2.0 implement onBackPressed() instead of doing this 
            //Handle the back button
            case KeyEvent.KEYCODE_BACK:
                if(lastBackKeyDown == -1)
                    lastBackKeyDown = e.getEventTime();
                return true; // not sent now
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                return true;
            case KeyEvent.KEYCODE_VOLUME_UP:
                return true;
            default:
                SDLActivity.sendKeyboardEvent(false, keyCode);
                return true;
        }
    }
    
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent e)
    {
        switch(keyCode)
        {
            case KeyEvent.KEYCODE_BACK:
                if(e.getEventTime() - lastBackKeyDown > 600) // long press
                {
                    game.confirmAndQuit();
                }
                else
                {
                    // need to send key down & up
                    SDLActivity.sendKeyboardEvent(false, keyCode);
                    SDLActivity.sendKeyboardEvent(true, keyCode);
                }
                lastBackKeyDown = -1;
                return true;
            case KeyEvent.KEYCODE_DPAD_DOWN:
            case KeyEvent.KEYCODE_DPAD_UP:
            case KeyEvent.KEYCODE_DPAD_RIGHT:
            case KeyEvent.KEYCODE_DPAD_LEFT:
                /* give some time for the game to scroll
                 * This is usefull only on devices that have not the ability to hold a key down (like the desire with its ) 
                 */
                game.sendKeyboardAsyncEvent(true, keyCode, 200);
                return true;
            default:
                SDLActivity.sendKeyboardEvent(true, keyCode);
                return true;
        }
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent e)
    {       
        /*
         * ScaleDectector is used to simulate mouse
         * 1-finger touch/tag -> left move/click
         * 2-fingers:
         *   move -> middle move/click
         *   rotate -> right click
         */
        scaleDetector.onTouchEvent(e);
        
        if(!scaleDetector.isInProgress() && e.getPointerCount() == 1)
        {
            final int action = e.getAction();
            final float x = e.getX();
            final float y = e.getY();    
            sendMouseEvent(action, SDLActivity.MOUSE_BUTTON_LEFT, x, y, 1f);
        }
        return true;
    }
}
